/**
 * @BelongsProject: hmall-micro
 * @BelongsPackage: com.hmall.trade.listener
 * @Author: 王明天
 * @CreateTime: 2025/2/13 20:12
 * @Description: PayLazyListener
 */
package com.hmall.trade.listener;



import com.hmall.common.constants.MQConstants;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.impl.OrderServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


/**
 * 监听订单支付超时消息
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PayLazyListener {

    private final OrderServiceImpl orderService;


    @RabbitListener (queues = MQConstants.DELAY_ORDER_QUEUE_NAME)
    public void listenDelayOrderPay(Long orderId) {
        //先查询订单是否已支付
        Order order = orderService.getById(orderId);


        if (order.getStatus() == 1) {
            orderService.lambdaUpdate().
                    eq(Order::getId, orderId)
                    .eq(Order::getStatus, 1)
                    .set(Order::getStatus, 5).update();
            //修改订单状态
            log.info("订单支付超时，已自动修改订单状态,订单号：{}", orderId);
            return;

        }

        log.info("订单已成功，未超时，订单号：{}", orderId);

    }


}
